package goods

import (
	"enjoy-shop/api/base"
	"enjoy-shop/formdata"
	"enjoy-shop/server/goods"
	"errors"
	"github.com/gin-gonic/gin"
)

func GetGood(c *gin.Context) {
	var r formdata.GoodsInfo
	if err := c.ShouldBindJSON(&r); err != nil {
		base.RespData(c, nil, err)
		return
	}
	if r.Id == 0 {
		base.RespData(c, nil, errors.New("id不存在"))
		return
	}
	info, err := goods.GetGoodsInfo(&r)

	base.RespData(c, info, err)
}

func SearchGoods(c *gin.Context) {
	var r formdata.GoodsInfo
	if err := c.ShouldBindJSON(&r); err != nil {
		base.RespData(c, nil, err)
		return
	}
	if r.Page < 1 {
		base.RespData(c, nil, errors.New("页码参数必须大于等于1"))
		return
	}
	if r.PageSize < 1 {
		base.RespData(c, nil, errors.New("每页数量参数必须大于等于1"))
		return
	}
	title, err := goods.SearchGoodsByTitle(&r)

	base.RespData(c, title, err)
}
func GoodList(c *gin.Context) {
	var r formdata.GoodList
	if err := c.ShouldBindJSON(&r); err != nil {
		base.RespData(c, nil, err)
		return
	}
	if r.Page < 1 {
		base.RespData(c, nil, errors.New("页码参数必须大于等于1"))
		return
	}
	if r.PageSize < 1 {
		base.RespData(c, nil, errors.New("每页数量参数必须大于等于1"))
		return
	}
	title, err := goods.GoodList(&r)

	base.RespData(c, title, err)
}
func GetSkuInfoByPVIDs(c *gin.Context) {
	var r formdata.GoodsSku
	if err := c.ShouldBindJSON(&r); err != nil {
		base.RespData(c, nil, err)
		return
	}
	if r.ProValueId1 == 0 || r.ProValueId2 == 0 || r.ProValueId3 == 0 {
		base.RespData(c, nil, errors.New("至少需要传入一个非空的PropertyValueId参数"))
		return
	}
	ds, err := goods.GetSkuInfoByPropertyValueIDs(&r)
	base.RespData(c, ds, err)
}
